/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.myapp.struts.servlets;

//~--- non-JDK imports --------------------------------------------------------
import com.myapp.struts.dao.Connect;
import com.myapp.struts.dao.JvDao;

//~--- JDK imports ------------------------------------------------------------

import java.io.*;

import java.util.*;

import javax.servlet.*;
import javax.servlet.http.*;

public class JvInsert extends HttpServlet {
    // instantiating Driver class

    public Connect drv = new Connect();
    // instance variable declaration
    public ArrayList al, alt;
    boolean bool;
    private RequestDispatcher rd;
    String str;

    // method that contains business logic
    @Override
    public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        // HttpServletRequestFacade rf=   (HttpServletRequestFacade)request;
        System.out.println(" INSIDE JvInsert..........");
        response.setContentType("text/html");

        PrintWriter out = response.getWriter();
        ServletContext application;
        String userid = "admin";

        // get the values from jsp
        String jvno = request.getParameter("jvno");
        String[] achead = request.getParameterValues("achead");
        String[] debit = request.getParameterValues("debit");
        String[] credit = request.getParameterValues("credit");
        String[] naration = request.getParameterValues("narration");
        String pd1 = request.getParameter("daydropdown");
        String pm1 = request.getParameter("monthdropdown");
        String py1 = request.getParameter("yeardropdown");
        String jvodate = py1 + "-" + pm1 + "-" + pd1;
        java.sql.Date myDate = new java.sql.Date(System.currentTimeMillis());
        String date = myDate.toString();

        try {
            System.out.println("inside try in JvInsert..");

            String Jv_Status = "1";
            Double debit1 = 0.0,
                    credit1 = 0.0;
            // Instantiating insert DAO at JvInsert Servlet
            for (int i = 0; i < achead.length; i++) {


                debit1 = debit1 + Double.parseDouble(debit[i]);
                credit1 = credit1 + Double.parseDouble(credit[i]);
                System.out.println("debit1..." + debit1);
                System.out.println("credit1..." + credit1);
            }
            if (debit1.equals(credit1)) {
                System.out.println("if(debit1==credit1)");

                for (int k = 0; k < achead.length; k++) {
                    JvDao jd = new JvDao();

                    bool = jd.insert(jvno, jvodate, achead[k], debit[k], credit[k], debit1, naration[k], userid,
                            date, Jv_Status);
                }
                if (bool == true) {
                    for (int k = 0; k < achead.length; k++) {
                        JvDao jd = new JvDao();
                        bool = jd.insertIntoLedger(jvno, jvodate, achead[k], debit[k], credit[k], debit1, Jv_Status);
                    }
                    if (bool == true) {
                        bool = true;
                    } else {
                        bool = false;
                    }
                }
            } else {
                bool = false;
            }


            System.out.println("BooleanValue.........." + bool);
            application = getServletContext();
            System.out.println("before if...");

            if (bool == true) {
                rd = request.getRequestDispatcher("com/myapp/home/success.jsp?code=120");
                rd.forward(request, response);
            } else {
                rd = request.getRequestDispatcher("com/myapp/home/fail.jsp?code=120");
                rd.forward(request, response);
            }
        } // end of try
        catch (Exception e) {
            System.out.println("CAUGHT AT JvInsert SERVLET" + e);
        } // end of catch
        finally {
            if (drv.mySql() != null) {
                try {
                    drv.mySql().close();
                    System.out.println("DATABASE CONNECTION TERMINATED");
                } // end of try
                catch (Exception e) {    /* ignore close errors */

                }                                // catch block close
            }                                    // end of if
        }                                        // end of finally
    }                                            // End of service method
}    // End of class

